package redis

import (
	"cart/cart"
	"context"
	"fmt"
	"strconv"
)

func CartAdd(GoodsId, UserId, Num int) error {
	key := fmt.Sprintf("cart:user_id_%d:goods_id_%d", UserId, GoodsId)
	return redisClient.HMSet(context.Background(), key, map[string]interface{}{
		"GoodsId": GoodsId,
		"UserId":  UserId,
		"Num":     Num,
	}).Err()
}
func CartList(GoodsId, UserId int) (cartlist []*cart.ListStruct) {
	key := fmt.Sprintf("cart:user_id_%d:goods_id_%d", UserId, GoodsId)
	keys := redisClient.Keys(context.Background(), key).Val()

	for _, v := range keys {
		all := redisClient.HGetAll(context.Background(), v).Val()
		GoodsId, _ = strconv.Atoi(all["GoodsId"])
		UserId, _ = strconv.Atoi(all["UserId"])
		Num, _ := strconv.Atoi(all["Num"])

		cartlist = append(cartlist, &cart.ListStruct{
			UserId:  int64(UserId),
			GoodsId: int64(GoodsId),
			Num:     int64(Num),
		})
	}
	return
}
